package com.googlecode.afdbaccess.web.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JsonPageUtil {
    public static void setAsJsonPage(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/json");
        request.setAttribute("isJsonPage", "true"); // This is a workaround for the fact that the set ContentType seems to be lost by a servlet/jsp forward, for instance to the error.jsp page
    }
    
    public static boolean isJsonPage(HttpServletRequest request, HttpServletResponse response) {
        return request.getHeader("Accept").startsWith("application/json") || // This can be made much smarter :)
          response.getContentType().startsWith("application/json") ||
          (request.getAttribute("isJsonPage") != null);
    }
}
